package com.mbschenkel.client;

import java.util.Map;
import java.util.TreeMap;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.mbschenkel.client.SnippetStruct;
import com.google.gwt.user.client.Random;

public class LatexSnippetManager extends Composite {
	VerticalPanel panel;
	
	private class SingleSnippetWidget extends HTML {
		private int id = 0;
		public SnippetStruct struct = null;
		private class SnippetReloadTimer extends Timer {
			public void run() {
				Session.getInstance().latexService.loadSnippet(
						Session.getInstance().getSessionData().getSessionVerificationData(),
						id, snippedLoadCallback);
			}
		}
		private SnippetReloadTimer retryTimer = new SnippetReloadTimer();
		private AsyncCallback<SnippetStruct> snippedLoadCallback  = new AsyncCallback<SnippetStruct>() {
			public void onFailure(Throwable caught) {
				Window.alert("RPC to snippedLoadCallback failed" + caught.getMessage() );
			}
			public void onSuccess(SnippetStruct result) {
				if( result == null ) {
					GWT.log("snippedLoadCallback success, but null received - abort, id = "+id, null);
					return;
				}
				
				GWT.log("snippedLoadCallback success, loaded "+id+":"+result.command, null);
				struct = result;
				if( result.tryReload ) {
					if( result.jobID != null ) {
						Session.getInstance().latexService.processJob(
								Session.getInstance().getSessionData().getSessionVerificationData(),
								result.jobID, new AsyncCallback<String>()
						{
							public void onFailure(Throwable caught){}
							public void onSuccess(String result){}
						});
					}
					retryTimer.schedule(500);
					display();
				}else{
					display();
				}
			}
		};
		public void display() {
			if(struct==null) {
				this.setHTML("loading..."+ com.google.gwt.user.client.Random.nextInt());
			}else{
				setHTML(struct.getHTML());
			}
		}
		public void reload() {
			Session.getInstance().latexService.loadSnippet(
					Session.getInstance().getSessionData().getSessionVerificationData(),
					id, snippedLoadCallback);
			display();
		}
		public SingleSnippetWidget( int id ) {
			this.id = id;
			reload();
						
		}
	}

	

	TreeMap <Integer, SingleSnippetWidget> snippetMap;

	AsyncCallback< SnippetList > loadSnippetListCallback = new AsyncCallback< SnippetList >() {
    	public void onFailure(Throwable caught) {
    		Window.alert("RPC to loadsnippetlist failed : " + caught.getMessage());
    	}
    	public void onSuccess(SnippetList result) {
    		GWT.log("RPC to load snippetlist onSuccess ",null);
    		for(int i=0;i<result.size();i++){
    			int id = result.get(i);
    			SingleSnippetWidget item = new SingleSnippetWidget(id);
    			snippetMap.put(id, item);
    			panel.add(item);
    		}   	
    	}
    };
    
	LatexSnippetManager() {
		Session.getInstance().latexService.loadSnippetList( 
				Session.getInstance().getSessionData().getSessionVerificationData(),
				loadSnippetListCallback );
		panel = new VerticalPanel();
		snippetMap = new TreeMap<Integer,SingleSnippetWidget>();
		panel.setWidth("100%");
		panel.add( new HTML("Create new snippet: ___"));
		initWidget(panel);
		setWidth("100%"); //must be after initWidget
		
	}

}
